<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores/userStore'
import { ElMessage } from 'element-plus'
import router from '@/router';

const userStore = useUserStore()

const form = ref({
  account: '13888888888',
  password: '123456',
  agree: true
})
const rules = {
  account: [
    { required: true, message: '用户名不能为空', trigger: 'blur'}
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min:6, max: 14, message: '密码长度为6-14个字符', trigger: 'blur'}
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (value) {
          callback()
        } else {
          callback(new Error('请勾选协议'))
        }
      }
    }
  ]
}
const formRef = ref(null)

const doLogin = () => {
  const { account, password } = form.value
  formRef.value.validate(async(valid) => {
    if (valid) {
      await userStore.getUserInfo({ account, password })
      ElMessage({ type: 'success', message: '登录成功' })

      router.replace({ path: '/'})
    }
  })
}
</script>

<template>
  <div>
    <!--头部logo-->
    <header class="login-header">
      <div class="container">
        <h1 class="logo">
          <router-link to="/">小兔鲜</router-link>
        </h1>
        <router-link class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </router-link>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账户登录</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <div class="form">
              <el-form ref="formRef" :model="form" :rules="rules"
              label-position="right" label-width="60px" status-icon>
              <el-form-item prop="account" label="账户">
                <el-input v-model="form.account"></el-input>
              </el-form-item>
              <el-form-item prop="password" label="密码">
                <el-input v-model="form.password"></el-input>
              </el-form-item>
              <el-form-item prop="agree" label-width="22px">
                <el-checkbox size="large" v-model="form.agree">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn" @click="doLogin">点击登录</el-button>
            </el-form>
            </div>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 小兔鲜儿</p>
      </div>
    </footer>
  </div>
</template>

<style lang="scss">
.login-header {
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;
  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  .logo {
    width: 200px;
    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -99999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }
  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}
.login-section {
  height: 488px;
  position: relative;
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;

  .wrapper {
    width: 380px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba($color: #000000, $alpha: 0.15);
    
    nav {
      font-size: 18px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      align-items: center;
      justify-content: center;
      
    }

    .form {
      padding: 0 20px 10px 20px;
      
      .subBtn {
        background-color: $xtxColor;
        width: 100%;
        color: #fff;
      }
    }
  }
}
.login-footer {
  padding: 30px 0 50px;
  background-color: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      
      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}
</style>